/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conversormonetario;

import javax.swing.JOptionPane;

/**
 *
 * @author Admin1
 */
public class ConversorMonetario extends javax.swing.JFrame {

    /**
     * Creates new form ConversorMonetario
     */
    ListaMoedas lista;
    public ConversorMonetario() 
    {
       initComponents();
       lista = new ListaMoedas(1);
       //lista.ActualizarTaxas();
       //lista.GravaDados();
       
       
       Box2.setSelectedIndex(0);
       Box3.setSelectedIndex(1);
       Box1.setSelectedIndex(0);
       
       CaixaTime.setText(lista.Ultima_Actualizacao);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Box1 = new javax.swing.JComboBox();
        Box2 = new javax.swing.JComboBox();
        Box3 = new javax.swing.JComboBox();
        Caixa1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        Caixa2 = new javax.swing.JTextField();
        CaixaTime = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Conversor Monetario");

        Box1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EUR to GBP", "EUR to USD", "GBP to EUR", "USD to EUR", "Outras" }));
        Box1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                Box1ItemStateChanged(evt);
            }
        });

        Box2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EUR", "GBP", "USD", "AUD", "CAD", "CHF", "CNY", "HKD", "IDR", "INR", "JPY", "THB", "ALL", "DZD", "ARS", "AWG", "BSD", "BHD", "BDT", "BBD", "BZD", "BMD", "BTN", "BOB", "BWP", "BRL", "BND", "BGN", "CVE", "KYD" }));
        Box2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                Box2ItemStateChanged(evt);
            }
        });

        Box3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EUR", "GBP", "USD", "AUD", "CAD", "CHF", "CNY", "HKD", "IDR", "INR", "JPY", "THB", "ALL", "DZD", "ARS", "AWG", "BSD", "BHD", "BDT", "BBD", "BZD", "BMD", "BTN", "BOB", "BWP", "BRL", "BND", "BGN", "CVE", "KYD" }));
        Box3.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                Box3ItemStateChanged(evt);
            }
        });

        Caixa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Caixa1ActionPerformed(evt);
            }
        });
        Caixa1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Caixa1KeyReleased(evt);
            }
        });

        jButton1.setText("Historico");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        Caixa2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Caixa2ActionPerformed(evt);
            }
        });
        Caixa2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Caixa2KeyReleased(evt);
            }
        });

        CaixaTime.setText("jLabel2");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/conversormonetario/simbolo.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Box1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(Caixa1)
                                    .addComponent(Caixa2, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Box3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Box2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(46, 46, 46))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(CaixaTime, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(Box1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Caixa1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Box2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Box3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Caixa2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(CaixaTime)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Caixa2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Caixa2ActionPerformed
       
       
    }//GEN-LAST:event_Caixa2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void Caixa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Caixa1ActionPerformed
       
    }//GEN-LAST:event_Caixa1ActionPerformed

    private void Caixa1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Caixa1KeyReleased
       String temp = Caixa1.getText();
        for(int i=0;i<temp.length();i++)
        {
            if(temp.length()<=10)
            {
                if(temp.charAt(i)== '.' && conta_ocorrencias('.',temp)==1)
                    ;
                else
                {
                    if(!Character.isDigit(temp.charAt(i)))
                    {

                        Caixa1.setText(temp.substring(0,temp.length()-1));
                        Caixa1.requestFocus();
                    }
                }
            }
            else
            {
                Caixa1.setText(temp.substring(0,temp.length()-1));
                Caixa1.requestFocus();
            }
        }     
        String de = Box2.getSelectedItem().toString();
        String para = Box3.getSelectedItem().toString();
        double cal = lista.getConvercao(de, para, Double.parseDouble(temp));
        Caixa2.setText("" + cal);
    }//GEN-LAST:event_Caixa1KeyReleased

    private void Caixa2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Caixa2KeyReleased
        String temp = Caixa2.getText();
        for(int i=0;i<temp.length();i++)
        {
            if(!Character.isDigit(temp.charAt(i)))
            {
                JOptionPane.showInputDialog(this,"Caractere");
                Caixa2.setText(temp.substring(0,temp.length()-1));
                Caixa2.requestFocus();
            }
        }     
        String de = Box2.getSelectedItem().toString();
        String para = Box3.getSelectedItem().toString();
        double cal = lista.getConvercao(para, de, Double.parseDouble(temp));
        Caixa1.setText("" + cal);
    }//GEN-LAST:event_Caixa2KeyReleased

    private void Box1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_Box1ItemStateChanged
        
        switch (Box1.getSelectedIndex()) 
        {
            case 0:  
                Box2.setSelectedIndex(0);
                Box3.setSelectedIndex(1);
                break;
            case 1:  
                Box2.setSelectedIndex(0);
                Box3.setSelectedIndex(2);
                break;
            case 2:  
                Box2.setSelectedIndex(1);
                Box3.setSelectedIndex(0);
                break;
            case 3:  
                Box2.setSelectedIndex(2);
                Box3.setSelectedIndex(0);
                break;
            default: 
                 break;
        }
    }//GEN-LAST:event_Box1ItemStateChanged

    private void Box2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_Box2ItemStateChanged
        Box1.setSelectedIndex(4);
    }//GEN-LAST:event_Box2ItemStateChanged

    private void Box3ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_Box3ItemStateChanged
        Box1.setSelectedIndex(4);
    }//GEN-LAST:event_Box3ItemStateChanged

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        Historico h =new Historico();
        h.setVisible(true);
    }//GEN-LAST:event_jButton1MouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConversorMonetario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConversorMonetario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConversorMonetario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConversorMonetario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ConversorMonetario().setVisible(true);
            }
        });
    }
    
    
    public static int conta_ocorrencias(char caracter ,String str)
    {     
        int count=0;     
        for (int i=0;i<str.length();i++){     
            if(str.charAt(i)==caracter){     
                count++;     
            }     
        }     
        return count;     
    }  


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox Box1;
    private javax.swing.JComboBox Box2;
    private javax.swing.JComboBox Box3;
    private javax.swing.JTextField Caixa1;
    private javax.swing.JTextField Caixa2;
    private javax.swing.JLabel CaixaTime;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
